﻿using System;
using UnityEngine;

namespace Doudizhu
{
    public class ActionController : MonoBehaviour
    {
        private GameObject _timer;
        private GameObject _bujiao;
        private GameObject _jiaodizhu;
        private GameObject _buqiang;
        private GameObject _qiangdizhu;
        private GameObject _bujiabei;
        private GameObject _jiabei;
        private GameObject _chaojijiabei;
        private GameObject _buchu;

        private TimerController _timerController;

        private void Awake()
        {
            _timer = transform.Find("Timer").gameObject;
            _bujiao = transform.Find("BuJiao").gameObject;
            _jiaodizhu = transform.Find("JiaoDiZhu").gameObject;
            _buqiang = transform.Find("BuQiang").gameObject;
            _qiangdizhu = transform.Find("QiangDiZhu").gameObject;
            _bujiabei = transform.Find("BuJiaBei").gameObject;
            _jiabei = transform.Find("JiaBei").gameObject;
            _chaojijiabei = transform.Find("ChaoJiJiaBei").gameObject;
            _buchu = transform.Find("BuChu").gameObject;

            _timerController = _timer.GetComponent<TimerController>();
        }

        private void Start()
        {
            hideAll();
        }

        public void ShowAction(ACTION action)
        {
            hideAll();
            switch (action)
            {
                case ACTION.timer: _timer.SetActive(true); break;
                case ACTION.bujiao: _bujiao.SetActive(true); break;
                case ACTION.jiaodizhu: _jiaodizhu.SetActive(true); break;
                case ACTION.buqiang: _buqiang.SetActive(true); break;
                case ACTION.qiangdizhu: _qiangdizhu.SetActive(true); break;
                case ACTION.bujiabei: _bujiabei.SetActive(true); break;
                case ACTION.jiabei: _jiabei.SetActive(true); break;
                case ACTION.chaojijiabei: _chaojijiabei.SetActive(true); break;
                case ACTION.buchu: _buchu.SetActive(true); break;
                case ACTION.undefine: break;
                default: break;
            }
        }

        public void HideAction(ACTION action)
        {
            switch (action)
            {
                case ACTION.timer: _timer.SetActive(false); break;
                case ACTION.bujiao: _bujiao.SetActive(false); break;
                case ACTION.jiaodizhu: _jiaodizhu.SetActive(false); break;
                case ACTION.buqiang: _buqiang.SetActive(false); break;
                case ACTION.qiangdizhu: _qiangdizhu.SetActive(false); break;
                case ACTION.bujiabei: _bujiabei.SetActive(false); break;
                case ACTION.jiabei: _jiabei.SetActive(false); break;
                case ACTION.chaojijiabei: _chaojijiabei.SetActive(false); break;
                case ACTION.buchu: _buchu.SetActive(false); break;
                case ACTION.undefine:
                default: hideAll(); break;
            }
        }

        public void StartTimer(int time, Action action)
        {
            _timer.SetActive(true);
            if (action != null)
            {
                action += () => { HideAction(ACTION.timer); };
            }
            _timerController.StartTimer(time, action);
        }

        public void StopTimer()
        {
            _timerController.StopTimer();
            _timer.SetActive(false);
        }

        private void hideAll()
        {
            _timer.SetActive(false);
            _bujiao.SetActive(false);
            _jiaodizhu.SetActive(false);
            _buqiang.SetActive(false);
            _qiangdizhu.SetActive(false);
            _bujiabei.SetActive(false);
            _jiabei.SetActive(false);
            _chaojijiabei.SetActive(false);
            _buchu.SetActive(false);
        }
    }
}
